// pages/new/new.js
const app = getApp()
var util = require('../../utils/util.js');
import { HTTP } from '../../utils/https.js'
let http = new HTTP()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    chatList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function (options) {
    this.chatList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  chatList:function(){
    var that= this;
    http.request({
      url: 'mini/v1/chat/list',
      
      success: function (res) {
        wx.hideLoading();
        wx.stopPullDownRefresh()
        const data = res.data
        for (var i = 0; i < data.length; i++) {
          data[i].created_at = util.timestampToTime(data[i].created_at),
          data[i].text = JSON.parse(data[i].text)
        } 
        that.setData({
          chatList:data
        })
      }
    })
  },

//读取消息
  readnew:function(e){
    const [id, that,receiveuser_id,name] = [e.currentTarget.dataset.id, this,e.currentTarget.dataset.receiveuser_id,e.currentTarget.dataset.name];
    http.request({
      url: 'mini/v1/chat/read',
      data:{user_id:receiveuser_id},
      success: function () {
        wx.navigateTo({
          url: '../../other/new_detail/new_detail?id='+receiveuser_id+'&&name='+name
        })
      }
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onPullDownRefresh: function () {
    this.chatList()
  },

  /**
   * 用户点击右上角分享·
   */
  onShareAppMessage: function () {
    return util.share()
  }
})